package com.order_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.order_system.constants.Code;
import com.order_system.dto.OrderListDto;
import com.order_system.entity.*;
import com.order_system.mapper.ManagerMapper;
import com.order_system.mapper.OrderListMapper;
import com.order_system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李阳
 * @since 2023-01-06
 */
@Service
public class OrderListServiceImpl extends ServiceImpl<OrderListMapper, OrderList> implements OrderListService {

    @Autowired
    private DetailListService detailListService;

    @Autowired
    private ProductService productService;

    @Autowired
    private PayModeService payModeService;

    @Override
    public ResModel getList(Integer customerId) {
        LambdaQueryWrapper<OrderList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,OrderList::getCustomerId,customerId);
        List<OrderList> list = list(wrapper);
        for (OrderList order : list) {
            PayMode payMode = payModeService.selectById(order.getModeId());
            order.setPayMode(payMode);

            LambdaQueryWrapper<DetailList> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(true,DetailList::getOrderId,order.getOrderId());
            List<DetailList> detailLists = detailListService.list(wrapper1);
            List<Product> productList = new ArrayList<>();
            for (DetailList detailList : detailLists) {
                Product product = productService.selectById(detailList.getProductId());
                productList.add(product);
            }
            order.setProduct(productList);
        }
        return ResModel.success(Code.SUCCESS,list);
    }

    @Override
    public ResModel getOrderList() {
        List<OrderList> list = list();
        for (OrderList order : list) {
            PayMode payMode = payModeService.selectById(order.getModeId());
            order.setPayMode(payMode);
            LambdaQueryWrapper<DetailList> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(true,DetailList::getOrderId,order.getOrderId());
            List<DetailList> detailLists = detailListService.list(wrapper1);
            List<Product> productList = new ArrayList<>();
            for (DetailList detailList : detailLists) {
                Product product = productService.selectById(detailList.getProductId());
                productList.add(product);
            }
            order.setProduct(productList);
        }
        return ResModel.success(Code.SUCCESS,list);
    }

    @Override
    public ResModel add(OrderListDto orderListDto) {
        List<Integer> ids = orderListDto.getIds();
        double totalPrice=0;
        for (Integer id : ids) {
            Product product = productService.selectById(id);
            totalPrice+=product.getPrice();
        }
        OrderList orderList = new OrderList();
        orderList.setModeId(orderListDto.getModeId());
        orderList.setTotalPrice(totalPrice);
        orderList.setOrderTime(new Date());
        orderList.setCustomerId(orderListDto.getCustomerId());
        boolean save = save(orderList);
        LambdaQueryWrapper<OrderList> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(true,OrderList::getCustomerId,orderListDto.getCustomerId());
        wrapper.eq(true,OrderList::getTotalPrice,totalPrice);
        wrapper.eq(true,OrderList::getModeId,orderListDto.getModeId());
        OrderList one = getOne(wrapper);
        for (Integer id : ids) {
            DetailList detailList = new DetailList();
            detailList.setCount(1);
            detailList.setProductId(id);
            detailList.setOrderId(one.getOrderId());
            detailListService.save(detailList);
        }
        return ResModel.success(Code.SUCCESS,save);
    }
}
